package ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fy.fyzf.R;
import com.fy.fyzf.bean.HousesListBean;
import com.fy.fyzf.glideUtils.GlideUtils;
import com.fy.fyzf.utils.MathUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.http.POST;

/**
 * 首页列表
 */
public class MatchHousingAdapter extends BaseQuickAdapter<HousesListBean.ListBean, BaseViewHolder> {

    private Context mContext;
    private int mType;
    private boolean mulitSelect;
    private String price;

    // boolean  是否多选
    public MatchHousingAdapter(Context context, int type, boolean b) {
        super(R.layout.item_match_house);
        mContext = context;
        mType = type;
        mulitSelect = b;
        init();
    }

    private int selectPos = -1;

    public void setSelect(int i) {
        selectPos = i;
        notifyDataSetChanged();
    }

    public Map<Integer, Boolean> selectMap = new HashMap<>();


    // 初始化 设置所有item都为未选择
    public void init() {
       /* selectMap = new HashMap<Integer, Boolean>();
        List<HousesListBean.ListBean> data = getData();
        for (int i = 0; i < getData().size(); i++) {
            selectMap.put(i, false);
        }
*/

    }
    @Override
    protected void convert(BaseViewHolder helper, HousesListBean.ListBean item) {
        helper.addOnClickListener(R.id.share_wx);
        helper.addOnClickListener(R.id.share_pyq);
        helper.addOnClickListener(R.id.iv_star);
        helper.setText(R.id.tv_name, item.getFloorName() + " " + item.getHousingNo());
        CheckBox ivSelect = helper.getView(R.id.iv_select);
        ImageView imageView = helper.getView(R.id.iv_star);
        if (mType == 1) {
            imageView.setVisibility(View.VISIBLE);
            ivSelect.setVisibility(View.GONE);
        } else {
            imageView.setVisibility(View.GONE);
            ivSelect.setVisibility(View.VISIBLE);
        }
        if (item.getCollection() == null) {
            imageView.setBackgroundResource(R.mipmap.star_gray);
        } else {
            imageView.setBackgroundResource(R.mipmap.icon_star);
        }
        if (!mulitSelect) {

            if (selectPos == helper.getAdapterPosition()) {
                ivSelect.setChecked(true);
            } else {
                ivSelect.setChecked(false);
            }
        } else {
//            selectMap.put(helper.getAdapterPosition(), item.isCheck());
            ivSelect.setChecked( item.isCheck());
        }

        helper.setText(R.id.tv_address, item.getHousingArea() + "m² | " + item.getFloorName());
        String s;
        if (null == item.getRentPrice()) {
            price = "0";
        } else {
            price = MathUtils.rvZeroAndDot(item.getRentPrice());
        }
        if (item.getHousingPurpose() == 0) {
            s = price + "元/m²/天(租)";
        } else if (item.getHousingPurpose() == 1) {
            s = price + "元/m²/天(售)";
        } else {
            s = price + "元/m²/天(可租可售)";
        }

        switch (item.getRenovationType()) {
            case 1:
                helper.setText(R.id.tv_status, "简装");
                break;
            case 2:
                helper.setText(R.id.tv_status, "精装");
                break;
            case 3:
                helper.setText(R.id.tv_status, "豪装");
                break;
        }
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(s);
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#DF2627")), 0, price.length(), spannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#353535")), price.length(), s.length(), spannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
        helper.setText(R.id.tv_price, spannableStringBuilder)
                .setText(R.id.tv_location, item.getParentAreaName() + item.getSubwayName() + "附近");
        GlideUtils.loadRoundedCornersImage(item.getHousingImage(), helper.getView(R.id.iv_logo), 5);
    }

    SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    private ArrayList<Integer> mList = new ArrayList<>();

    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }

    //获得选中条目的结果
    public ArrayList<Integer> getSelectedItem() {
        List<HousesListBean.ListBean> data = getData();
        mList.clear();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).isCheck()) {
                mList.add(data.get(i).getHousingId());
            }
        }
        return mList;
    }
}
